package com.netty.timeold;

import com.netty.timeold.model.BaseModel;
import com.sevenchildren.framework.entity.BaseResp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

public class TimeClient {
	String name = "";

	private Channel channel;

	TimeClientSimpleHandler handler;

	public TimeClient() {
	}

	public TimeClient(String name, int port, String host) {
		this.name = name;
		this.port = port;
		this.host = host;
		handler = new TimeClientSimpleHandler(name);
	}

	int port;
	String host;
	Bootstrap b;

	public void connect() {
		new Thread() {
			public void run() {
				try {
					connect0();
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来我退出来");
			};
		}.start();
	}

	public void connect0() throws Exception {

		EventLoopGroup group = new NioEventLoopGroup();
		try {
			b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(new IdleStateHandler(0, 0, 5));
					ch.pipeline().addLast(new LengthFieldPrepender(8));
					ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 8, 0, 8));
					ch.pipeline().addLast(new StringDecoder());
					ch.pipeline().addLast(new StringEncoder());
					ch.pipeline().addLast(handler);
				}
			});

			connect1();

		} finally {
			group.shutdownGracefully();
		}
	}

	public void connect1() throws InterruptedException {
		if (channel != null && channel.isActive()) {
			return;
		}

		ChannelFuture future = b.connect(host, port).sync();

		future.addListener(new ChannelFutureListener() {
			public void operationComplete(ChannelFuture futureListener) throws Exception {
				if (futureListener.isSuccess()) {
					channel = futureListener.channel();
					System.out.println("Connect to server successfully!");
				}
			}
		});

		future.channel().closeFuture().sync();
		System.out.println("---f.channel().closeFuture().sync()");

	}

	public BaseResp req(BaseModel model) {
		return handler.req(model);
	}

	public static void main(String[] args) throws Exception {
		TimeClient tc = new TimeClient("CCC", 8080, "127.0.0.1");
		tc.connect();

		// System.out.println("-1-");
		// TimeUnit.SECONDS.sleep(200000);
		// System.out.println("-2-");
		// BaseResp br = tc.req(new TimeTestReq());
		// System.out.println(br);
	}

}
